//
//  TestCollectImageCVC2.swift
//  CooperSwift_Example
//
//  Created by 计海峰 on 2021/12/9.
//  Copyright © 2021 CocoaPods. All rights reserved.
//

import UIKit
import Kingfisher
import CooperSwift
import RxSwift

class TestCollectImageCVC2Model: NSObject, CollectionCellConfigProtocol, BindKeyProtocol {

    func cellCls() -> UICollectionViewCell.Type {
        return TestCollectImageCVC2.self
    }
    
    var model: Any?
    
    required init(_ model: Any?) {
        super.init()
        self.model = model
        self.cellSize = CGSize((Screen.width - 32)/3.0 - 0.1, (Screen.width - 32)/3.0 - 0.1)
    }
    
}

class TestCollectImageCVC2: UICollectionViewCell {

    @IBOutlet weak var iconIMG: UIImageView!
    
    let disposeBag = DisposeBag()
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        iconIMG.isUserInteractionEnabled = true
        let tap = UITapGestureRecognizer()
        iconIMG.addGestureRecognizer(tap)
        tap.rx.event.subscribe(onNext: { [weak self] _ in
            guard let model = self?.info as? TestCollectImageCVC2Model else { return }
            guard let url = model.model as? String else { return }
            showPhotosBrowser([url])
        }).disposed(by: disposeBag)
    }
    
    override func bindTo(_ info: Any?) {
        super.bindTo(info)
        guard let model = info as? TestCollectImageCVC2Model else { return }
        iconIMG.kf.setImage(with: URL(string: model.model as! String))
    }

    @IBAction func deleteTap(_ sender: Any) {
        guard let cellModel = self.info as? TestCollectImageCVC2Model else { return }
        cellModel.postCollectionService(.removeItem)
    }

}
